﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sustainalytics.ESGRatings.BusinessLogic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Sustainalytics.ClientDW.DataAccessLayer.ESGRatings.Fakes;
using Sustainalytics.ClientDW.DataAccessLayer.Fakes;
using Sustainalytics.ClientDW;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.DataLayer.ESGRatings.Fakes;
using Sustainalytics.Entities.ESGRatings;
using Sustainalytics.EsgRatings.BusinessLogic;
using Sustainalytics.ESGRatings.RedisCache.Fakes;
using Sustainalytics.ESGRatings.RedisCache;
using Sustainalytics.Synchronization.Interface;


namespace Sustainalytics.ESGRatings.BusinessLogic.Tests
{

    //todo Petre add more tests
    [TestClass()]
    public class RawScoresSyncStepTests
    {
     

        //[TestMethod()]
        //public void SyncRawScoresTestCreate()
        //{


        //    // input data 
        //    var sb = new StringBuilder();
        //    var esgCompanies = new List<CorporateData>
        //    {
        //        new CorporateData
        //        {
        //            Id = Guid.NewGuid(), 
        //            CapitalIQID = "IQ1", 
        //            HasEsgResearch = true, 
        //            RootPeerGroup = "PeerGroup1", 
        //            CompanyType = "Public", 
        //            TemplateType = "Type A"
        //        }, 
        //        new CorporateData
        //        {
        //            Id = Guid.NewGuid(), 
        //            CapitalIQID = "IQ2", 
        //            HasEsgResearch = true, 
        //            RootPeerGroup = "PeerGroup2", 
        //            CompanyType = "Public", 
        //            TemplateType = "Type B"
        //        }, 
        //        new CorporateData
        //        {
        //            Id = Guid.NewGuid(), 
        //            CapitalIQID = "IQ3", 
        //            HasEsgResearch = true, 
        //            RootPeerGroup = "PeerGroup2", 
        //            CompanyType = "Public", 
        //            TemplateType = "Type B"
        //        }, 
        //        new CorporateData
        //        {
        //            Id = Guid.NewGuid(), 
        //            CapitalIQID = "IQ4", 
        //            HasEsgResearch = true, 
        //            RootPeerGroup = "PeerGroup1", 
        //            CompanyType = "Private", 
        //            TemplateType = "Type B"
        //        }
        //    };            

        //    var rpinds =  new List<RPIndicator>();                                
        //    esgCompanies.ForEach(x =>
        //    {
        //        rpinds.Add(new RPIndicator(){  CompanyId = x.Id, Id = Guid.NewGuid(), Number = "E.1.1", SelectedAnswer = new AnswerCategory() { Score = 10 } });
        //        rpinds.Add(new RPIndicator() { CompanyId = x.Id, Id = Guid.NewGuid(), Number = "E.1.2", SelectedAnswer = new AnswerCategory() { Score = 20 } });
        //        rpinds.Add(new RPIndicator() { CompanyId = x.Id, Id = Guid.NewGuid(), Number = "E.1.3", SelectedAnswer = new AnswerCategory() { Score = 30 } });
        //        rpinds.Add(new RPIndicator() { CompanyId = x.Id, Id = Guid.NewGuid(), Number = "E.1.4", SelectedAnswer = new AnswerCategory() { Score = 40 } });
        //        rpinds.Add(new RPIndicator() { CompanyId = x.Id, Id = Guid.NewGuid(), Number = "E.1.5", SelectedAnswer = new AnswerCategory() { Score = 50 } });
        //    });
                      
        //    var prev = esgCompanies.Select(x => new ChangedElement()
        //    {
        //        AffectedObjectName = "CompanyId",
        //        Status = SyncResultStatus.AppliedSuccess,
        //        AffectedDocumentId = x.Id
        //    }).ToList();


        //    // output data 
        //   var cachedCompanies = new List<CompanyScores>();
        //   var updatedKeys = new List<string>();
        //   var writtenHistory = new List<Entities.RawScoreAndWeightsHistory>();

        //    // objects 
        //    var clientDw = new StubIClientDwRepository
        //    {
               
        //        ListCompanyRpIndicatorsGuid = id => rpinds.Where(x => x.CompanyId == id).ToList(),
        //        ListCompaniesCorporateDataIEnumerableOfGuid = companiesIds => esgCompanies.Where(c => companiesIds.Contains(c.Id)).ToList()
        //    };
        //    var redisCache = new StubIESGRatingsDistributedCache
        //    { 
        //         NotifyUpdatedKeysListOfString =(incoming) =>
        //         {
        //             updatedKeys = incoming;
        //         }
        //    };

        //    redisCache.Add2SetOf1M0<CompanyScores>((CompanyScores sc) => { cachedCompanies.Add(sc); });
        //    redisCache.SetOf1M0<CompanyScores>((CompanyScores sc) => 
        //    {
        //        cachedCompanies.Add(sc);
        //        return KeysGenerator.Key<CompanyScores>(sc);
        //    });
        //    var historyRep = new Sustainalytics.DataLayer.Fakes.StubIRawScoreAndWeightsHistoryRepository()
        //    {
        //        CreateBatchIEnumerableOfRawScoreAndWeightsHistory = (IEnumerable<Entities.RawScoreAndWeightsHistory> toWrite)=>
        //        {
        //            writtenHistory = toWrite.ToList();
        //            return true;
        //        },

        //        ReadByCompanyIdGuid  = (Guid id) =>
        //        {
        //            return new List<Entities.RawScoreAndWeightsHistory>();
        //        }
        //    };
        //    var weightMatricesRep = new StubIWeightMatricesRepository()
        //    {
        //        ListCompanyWeightsByCompanyIdsListOfGuid = (List<Guid> companyids) =>
        //       {
        //          // rpinds.Select(x => x.Number).ToList() 
        //           var weights =  new Dictionary<string,float>();
        //           weights["E.1.1"] = .2F;
        //           weights["E.1.2"] = .2F;
        //           weights["E.1.3"] = .2F;
        //           weights["E.1.4"] = .2F;
        //           weights["E.1.5"] = .2F;



        //           var res = rpinds
        //               .Select(x => x.CompanyId)
        //               .Distinct().ToList()
        //               .Select(x => new CompanyWeights() { Id = Guid.NewGuid(), CompanyId = x, MatrixId = Guid.Empty, Weights = weights })
        //               .ToList();

        //               return res; 


        //           //return null;
        //       },
        //       GetWeightMatrixByNameString = (string name) =>
        //       {
        //            if (name == "Default Weight Matrix")
        //            {
        //                var result = new WeightMatrix() { Id = Guid.Empty, IndicatorsNumbers = new List<string>(), Name = "Default Weight Matrix" };

        //                result.IndicatorsNumbers = rpinds.Select(x => x.Number).Distinct().ToList();
        //                return result;
        //            }
        //            return null;
        //       }
        //    };


          

        //    var rawpProc = new RawScoresProcessor(clientDw, historyRep, redisCache, weightMatricesRep, sb, "");
        //    var current = rawpProc.SyncRawScores(prev);


        //    // check cached companies  and written keys 
        //    Assert.AreEqual(updatedKeys.Count(), esgCompanies.Count(), " companies count mismatch");
        //    Assert.AreEqual(updatedKeys[0], "CompanyScores" + esgCompanies[0].Id.ToString(), " companies values  mismatch");
        //    Assert.AreEqual(cachedCompanies[3].Id, esgCompanies[3].Id, " companies values  mismatch");
            
        //    var m = rpinds.Where( x=> x.CompanyId == esgCompanies[3].Id && x.SelectedAnswer.Score == 20).First();
        //    Assert.AreEqual(cachedCompanies[3].IndicatorsRawScoreWeight[1].IndicatorNumber, m.Number , " companies number values  mismatch");
        //    Assert.AreEqual(cachedCompanies[3].IndicatorsRawScoreWeight[1].RawScore, m.SelectedAnswer.Score, " companies scores values  mismatch");

        //   // check history
        //    Assert.AreEqual(writtenHistory.Count() , rpinds.Count(), "history count mismatch");
        //    Assert.AreEqual(writtenHistory.Count( x => x.IndicatorNumber=="E.1.2"), esgCompanies.Count(), "history count mismatch");
            
        //    // check current changes 
        //    Assert.AreEqual(current.Count(), writtenHistory.Count(), "current changes count mismatch");
            
        //}
    }
}
